#include "e7convert.h"

/** Universal utility routines for type convertions
 *  from unix-v7 floppie to local gnu-c++
 */

/* NB:
 * block = array van bytes (b.v. een diskblock),
 * offset = positie in het block
 */

/**
 * AANPASSING: offset wordt automatisch geadd
 */

// a 16 bit short int
short getshort(byte * block, int &offset) {
	unsigned int b1 = 0xFF & block[offset++];
	unsigned int b2 = 0xFF & block[offset];
	offset++;
	return (short) ((b1 << 8) + b2);
}

// a 24 bit half-long int
int getlong3(byte * block, int &offset) {
	int b1 = 0xFF & block[offset++];
	int b2 = 0xFF & block[offset++];
	int b3 = 0xFF & block[offset++];
	return (b1 << 16) + (b2 << 8) + b3;
}

// a 32 bit long int
long getlong(byte * block, int &offset) {
	return getlong4(block, offset);
}

// intern: 4-byte long/int ophalen
long getlong4(byte * block, int &offset) {
	int b1 = 0xFF & block[offset++];
	int b2 = 0xFF & block[offset++];
	int b3 = 0xFF & block[offset++];
	int b4 = 0xFF & block[offset];
	offset++;
	return (b1 << 24) + (b2 << 16) + (b3 << 8) + b4;
}

// een reeks van 'count' ascii characters
string getchars(byte * block, int &offset, int count) {
	std::string s = "";
	int off = offset;
	for (int i = 0; (i < count) && (block[off] != 0); ++i) {
		s += ((char) (block[off++]));
	}
	offset += count;
	return s;
}

//byte ophalen, because
byte getbyte(byte * block, int &offset) {
	byte b = block[offset];
	offset++;
	return b;
}

